
var inputList;
var blockUI;
$(document).ready(function () {
    inputList = $('#form_receiver input');

    $('#btn_add_receiver').click(function () {
        receiverSelectionDialog.recoverToLastSave();
        receiverSelectionDialog.show();
    });

    $('#btn_confirm').click(function () {
        receiverSelectionDialog.saveReceiver();
        var nameList = receiverSelectionDialog.getReceiverNames();

        var displayString = '';
        for (var i = 0; i < nameList.length; i++) {
            displayString += nameList[i] + '; ';
        }
        if (displayString !== '' && displayString.substr(-2, 2) === '; ')
            displayString.slice(-2);

        $('#input_receiver').val(displayString);

        receiverSelectionDialog.hide();
    });

    $('#btn_send_message').click(function () {
        var receiverId = receiverSelectionDialog.getReceiverStudentIds();
        if (receiverId.length === 0) {
            UIkit.modal.alert('请选择接收消息的学生');
            return;
        }

        var content = $('#input_content').val();
        if (content === ''){
            UIkit.modal.alert('消息内容不能为空');
            return;
        }

        blockUI = UIkit.modal.blockUI('正在发送消息...');
        $.ajax({
            url: '/api/teacher/send_message',
            data: JSON.stringify({
                receivers: receiverId,
                content: content
            }),
            contentType: 'application/json',
            type: 'POST'
        })
            .done(function () {
                blockUI.hide();
                UIkit.modal.confirm('发送成功，是否继续发送消息？\n选择「取消」则停留在本页面。', function () {
                    location.reload();
                });
            })
            .fail(function () {
                blockUI.hide();
                UIkit.modal.alert('发送失败，发生了错误');
            })
    })
});

var receiverSelectionDialog = {
    selectionList: [],

    saveReceiver: function () {
        this.selectionList = $('#dialog_receiver_selection input:checked');
    },

    getReceiverNames: function () {
        var nameList = [];
        for (var i = 0; i < this.selectionList.length; i++) {
            var input = this.selectionList[i];
            var name = $(input).next().text();
            nameList.push(name);
        }
        return nameList;
    },

    getReceiverStudentIds: function () {
        var idList = [];

        if (this.selectionList.length !== 0 ) {
            this.selectionList.each(function () {
                idList.push($(this).val())
            })
        }

        return idList;
    },

    recoverToLastSave: function () {
        $('#dialog_receiver_selection input:checkbox').prop('checked', false);

        if (this.selectionList.length === 0)
            return;

        this.selectionList.each(function () {
            $(this).prop('checked', true);
        })
    },

    show: function () {
        UIkit.modal('#dialog_receiver_selection').show();
    },

    hide: function () {
        UIkit.modal('#dialog_receiver_selection').hide();
    }
};